home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


Validate at Shutdown

A program should validate its data structures when it is about to exit. These shutdown tests can alert you to problems if the program has corrupted the data structures while it was running.

It is common for a complex program to corrupt its data structures but still continue running for a long time before encountering an error. If one developer corrupts the data but does not realize it, and another developer discovers the error much later, it may be difficult to tell how and when the data structure was contaminated. Shutdown validation routines can at least identify the session during which the data structures were damaged.

You can use the same techniques during shutdown as you can at startup. If the tests are fast, you can perform them all in both the development and final compiled programs. If the tests are slow, you can use conditional compilation or a test to see if the program has been compiled and then skip some or all of the tests in the compiled version. You can even present a form like the one shown in Figure 5.2 to let the developer determine which tests to run.

The following code shows how a program’s main form can display a data structure validation form before it unloads.

‘ Validate the data structures and prepare to unload.
Private Sub Form_Unload()
    ValidationForm.Show vbModal
End Sub

If the program uses many forms, each can validate the data before it unloads. If the tests are relatively slow, each form can check to see if it is the last form before it performs validation.

‘ Validate the data structures and prepare to unload.
Private Sub Form_Unload()
    ‘ Validate the data structures if this
    ‘ is the last form running.
    If Forms.Count < 2 Then ValidationForm.Show vbModal
End Sub

Validate While Running

It is often useful to provide a means for developers to run data validation tests on demand. After you determine that a certain combination of commands corrupts the data, you can use the validation tests to help pinpoint the error.

Starting with a consistent data set, run the program. It should pass its startup tests because you are starting with fresh data. Next, perform the first in the series of operations that caused the corruption and run the data validation tests. Perform another step and validate the data again. Continue performing one step and then testing until you find the step that corrupts the data.

If you use a data validation form at startup or shutdown, you can use the same form while the program is running. A simple Test menu containing a Validate Data Structures command can display the form modally.

‘ Present the data structure validation form.
Private Sub mnuTestValidate()
    ValidationForm.Show vbModal
End Sub

You can also perform the tests interactively at any time while the program is stopped. Simply enter the command ValidationForm.Show vbModal in the Debug or Immediate window and Visual Basic will present the validation form. When you click the Ok button, the program will perform its validations exactly as if you had displayed the form using a menu command.

Validate Forms and Classes

You can extend the idea of data structure validation to cover forms and classes. When the program creates a new form or class object, that object’s Initialize event handler can validate the object’s data.

Unfortunately, initialize events do not take any parameters, so often the object cannot fully initialize itself during the Initialize event. For example, an Employee object cannot fill in its Name, EmployeeID, SocialSecurityNumber, and other data values during Initialize. Instead, it can provide the program with a public InitializeData subroutine that takes these values as parameters. After saving the data values, the routine can validate its data.

A form or class can also provide validation during its Terminate event, and it can provide a public method to allow the validation to run at any other times while the program is running. The validation routines can be helpful in tracking down an error that appears after a long series of operations on the objects.

Sometimes it is helpful to have the events of a specific object run a more global data validation routine. For example, when a program destroys an object, it may not have correctly removed that object from its data structures. The object’s Terminate event handler can run the program’s validation routines to verify that it has been correctly removed from the global data structure.

Clear Unused Variables

When the program is finished using a variable, it should clear it in a way that will expose bugs. Deallocate arrays using the Erase statement. Set object references and variants to Nothing. Give other variables strange values that you can recognize later. For example, set unused integers to –31,425. If you later find a function with a parameter that has value –31,425, you know there is a bug. The calling routine passed the function a variable that should not have been used again.

Setting object references to Nothing also removes the object from memory. As you set different variables to refer to a particular object, Visual Basic keeps track of the number of references to the object. When the reference count reaches zero, Visual Basic knows the program can no longer access the object so it frees the object’s memory. The object remains loaded in memory until its last reference goes out of scope or you set the last reference to Nothing.

This can be particularly confusing with forms. The Unload statement unloads a form and clears the form’s properties and controls. If the program tries to access any of these values, the program reloads the form and reinitializes all of these values. This can cause a lot of confusion.

For example, the following code creates an EmployeeForm and displays it. It then unloads the form and presents a message box showing the value displayed in the form’s txtName TextBox. This makes Visual Basic reload the form with newly initialized values so the text displayed will be whatever is displayed when the form is first loaded.

Later, when the user closes the program’s main form, this EmployeeForm is still loaded. Since a Visual Basic program only ends when all forms are unloaded, the program continues running even though no forms are visible.

Private EmpFrm As EmployeeForm
    :
Private Sub ShowEmployeeForm()
    ‘ Create the form.
    Set EmpFrm = New EmployeeForm

    ‘ Display the form.
    EmpFrm.Show vbModal

    ‘Unload the form.
    Unload EmpFrm

    ‘ Display the txtName value. This reloads the form!
    MsgBox EmpFrm.txtName.Text
End Sub


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.